package game.players.states;

import game.ball.Possessed;
import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.SoccerPlayerCPU;
import game.players.cpu.teammate.WithoutBallLeftMovingCPUMate;
import game.players.cpu.teammate.WithoutBallRightMovingCPUMate;
import game.players.cpu.teammate.WithoutBallStationaryCPUMate;
import game.players.user.movements.WithBallStationaryUser;
import game.scene.MatchScene;

import com.uqbar.vainilla.colissions.CollisionDetector;

public abstract class CPUMateWithoutBall extends WithoutBallCPU {
	
	public CPUMateWithoutBall(SoccerPlayer player) {
		super(player);
	}

	public void takePossession(SoccerBall ball, SoccerPlayer player) {
		double advanceX = this.getPlayer().getX() + this.getPlayer().getAppearance().getWidth() - ball.getX() - ball.getRatio();
		double advanceY = this.getPlayer().getY() + this.getPlayer().getAppearance().getHeight() - ball.getY() - ball.getRatio();
		
		ball.moveNormally(advanceX, advanceY);
		
		player.setState(new WithBallStationaryUser(player));
		ball.setState(new Possessed(player));
		player.getScene().setUserPlayer(player);
		player.addUserPlayerMark();
	}

	public boolean collidesAgainstBall(SoccerBall ball, SoccerPlayer player) {
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
			ball.getX(), ball.getY(), ball.getRatio(),
			player.getX() + player.getAppearance().getWidth() - player.getFeetWidth(), 
			player.getY() + player.getAppearance().getHeight() - player.getFeetHeight(), 
			player.getFeetWidth(), player.getFeetHeight()
			);
	}
	
	@Override
	protected void changeToStationaryPosition() {
		this.getPlayer().setState(new WithoutBallStationaryCPUMate(this.getPlayer()));
	}
	
	@Override
	protected void changeToTheRivalArc() {
		MatchScene scene = this.getPlayer().getScene();
		scene.changeFromStationaryCPUToUser(scene.getUserPlayer(), (SoccerPlayerCPU) this.getPlayer());
	}
	
	@Override
	protected void changeToTheRivalArcWith(double advancedX, double advancedY) {
		this.getPlayer().setState(new WithoutBallRightMovingCPUMate(this.getPlayer(), advancedX, advancedY));
	}
	
	@Override
	protected void changeToRightMoving(double x, double y) {
		this.getPlayer().setState(new WithoutBallRightMovingCPUMate(this.getPlayer(), x, y));
	}
	
	@Override
	protected void changeToLeftMoving(double advancedX, double advancedY) {
		this.getPlayer().setState(new WithoutBallLeftMovingCPUMate(this.getPlayer(), advancedX, advancedY));
	}
	
	@Override
	protected boolean goesToTheRivalArc(double x) {
		return x <= 0;
	}
}
